{% extends 'base.html' %}
{% load humanize %}
{% block page-content %}
{% include 'nav_cat_bar.html' %}
{% load mytags %}

<script type="text/javascript">

    function validate_required(field,alerttxt)
    {
        with (field)
          {
          if (value==null||value=="")
            {alert(alerttxt);return false}
          else {return true}
          }
    }

    function validate_form(thisform)
    {
        with (thisform)
          {
          
         if(validate_required(script_use,"填写任务名称!")==false)
            {script_use.focus();return false}
          else if(validate_required(script_name,"填写脚本名称")==false)
            {script_name.focus();return false}

          else if(validate_required(minute,"分钟不能为空(每分钟用*)")==false)
            {minute.focus();return false}
          else if(validate_required(hour,"小时不能为空(每小时用*)")==false)
            {hour.focus();return false}
          else if(validate_required(week,"星期不能为空(每星期用*)")==false)
            {hour.focus();return false}
          else if(validate_required(date,"日期不能为空(每天用*)")==false)
            {hour.focus();return false}
          else if(validate_required(month,"月份不能为空(每月用*)")==false)
            {month.focus();return false}
          }
      
    }
</script>

<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>计划任务列表</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-wrench"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                            <li><a href="#">Config option 1</a>
                            </li>
                            <li><a href="#">Config option 2</a>
                            </li>
                        </ul>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    {% if log %}
                          <div class="alert alert-success text-center">{{ log }}</div>
                    {% endif %}
                    {% if err %}
                        <div class="alert alert-danger text-center">{{ err }}</div>
                    {% endif %}
                    <div class="row">
                        <div class="form-group pull-left  col-lg-2">
                        <button type="submit" class="btn btn-primary" data-toggle="modal" data-target="#new_script" data-whatever="@mdo">新建任务计划</button>
                        </div>
                        <div class="form-group pull-right col-lg-3">
                            <form id="checkform">
                                <div class="input-group">
                                        <input type="text" name="search_name" class="form-control"> <span class="input-group-btn">
                                        <button type="submit" form="checkform" formmethod="POST"  class="btn btn-primary">搜索</button> </span>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div class="table-responsive">
                      <form>
                        <table class="table table-bordered table-hover">
                            <thead>
                                <th class="text-center">项目</th>
                                <th class="text-center">任务名称</th>
                                <th class="text-center">脚本名称</th>
                                <th class="text-center">定时表达式</th>
                                <!--
                                <th class="text-center">创建时间</th>
                                <th class="text-center">创建人</th>
                                <th class="text-center">修改时间</th>
                                <th class="text-center">修改人</th>
                                -->
                                <th class="text-center">创建人</th>
                                <th class="text-center">已执行次数</th>
                                <th class="text-center">状态</th>
                                <th class="text-center">目标服务器</th>
                                <th class="text-center">操作</th>
                            </thead>
                            <tbody>
                                {% for num in menu %}
                                    <tr class="text-center">
                                        <td>{{num.project}}</td>
                                        <td>{{num.name}}</td>
                                        <td>{{num.script_name}}</td>
                                        <td>{{num.crontab}}</td>
                                        <!--
                                        <td>{{num.create_date|date:"Y-m-d H:i:s"}}</td>
                                        <td>{{num.create_user}}</td>
                                        <td>{{num.date_changed|date:"Y-m-d H:i:s"}}</td>
                                        <td>{{num.motify_user}}</td>
                                        -->
                                        <td>{{num.create_user}}</td>
                                        <td>{{num.total_run_count}}</td>

                                        {% if num.enabled %}
                                            <td><a class="btn btn-primary btn-xs" role="button" href="{% url 'task_plan_manage' %}?project={{num.project}}&script_name={{num.script_name}}&script_use={{num.name}}&action=switch">已启用</a></td>
                                        {% else %}
                                            <td><a class="btn btn-danger btn-xs" role="button" href="{% url 'task_plan_manage' %}?project={{num.project}}&script_name={{num.script_name}}&script_use={{num.name}}&action=switch">已停止</a></td>
                                        {% endif %}

                                        <td><a type="button" class="btn btn-xs btn-primary" href="{% url 'task_plan_hosts' %}?project={{project}}&script_use={{num.name}}&script_name={{num.script_name}}"><i class="fa fa-desktop"></i> 选择服务器</a>
                                        <a type="button" class="btn btn-xs btn-primary" href="{% url 'check_task_hosts' %}?project={{project}}&script_use={{num.name}}&script_name={{num.script_name}}"><i class="glyphicon glyphicon-eye-open"></i> 查看服务器</a></td>
                                        <td>
                                            {% ifequal user.is_admin 1 %}
                                            <a href="{% url 'task_plan_eye' %}?project={{num.project}}&id={{ num.id }}" class="btn btn-xs"><span class="glyphicon glyphicon-eye-open"></span> 审计</a>
                                            {% endifequal %}
                                            <a href="{% url 'task_plan_manage' %}?project={{num.project}}&script_name={{num.script_name}}&script_use={{num.name}}&action=edit" class="btn btn-xs"><span class="glyphicon glyphicon-pencil"></span> 编辑</a>
                                            <a href="{% url 'task_plan_manage' %}?project={{num.project}}&script_name={{num.script_name}}&script_use={{num.name}}&action=del" onclick="return confirm('确认删除吗？')"><span class="glyphicon glyphicon-trash"></span> 删除</a>
                                        </td>
                                    </tr>

                                {% endfor %}
                            </tbody>
                        </table>
                        <div class="col-sm-6">
                            <div class="dataTables_info" id="editable_info" role="status" aria-live="polite">
                                Showing {{ contacts.start_index }} to {{ contacts.end_index }} of {{ p.count }} entries
                            </div>
                        </div>
                        {% include 'paginator.html' %}
                      </form>
                    </div>


                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" role="dialog" aria-labelledby="gridSystemModalLabel" id="new_script">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="gridSystemModalLabel" align="center">新建任务计划</h4>
      </div>
      <div class="modal-body">
        <div class="container-fluid" align="center">
           <form id="assetForm" method="post" class="form-horizontal"  enctype="multipart/form-data" onsubmit="return validate_form(this)">
                <div class="form-group"><label class="col-sm-2 control-label">定时任务名称<span class="red-fonts">*</span></label>
                    <div class="col-sm-10">
                        <input type="text" name="script_use" class="form-control">
                    </div>
                </div>

                <div class="hr-line-dashed"></div>
                <div class="form-group"><label class="col-sm-2 control-label">选择执行脚本<span class="red-fonts">*</span></label>
                    <div class="col-sm-10">
                        <select class="form-control" name="script_name">
                            <option value= >请选择</option>
                            {% for num in script_files %}
                                <option value={{num.script_name}} >{{num.script_name}}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="hr-line-dashed"></div>
                <div class="form-group"><label class="col-sm-2 control-label"> 定时规则 <span class="red-fonts">*</span></label>
                    <div class="col-sm-10 input-group">
                        <div class="modal-body">
                              <div class="form-group">
                                  <label class="col-sm-2 control-label">分钟<span class="red-fonts"></span></label>
                                  <div class="col-sm-5">
                                      <input  name="minute" placeholder="*" type="text" class="form-control" >
                                  </div>
                              </div>
                              <div class="hr-line-dashed"></div><br>
                              <div class="form-group">
                                  <label class="col-sm-2 control-label">小时<span class="red-fonts"></span></label>
                                  <div class="col-sm-5">
                                      <input  name="hour" placeholder="*" type="text" class="form-control" >
                                  </div>
                              </div>

                              <div class="hr-line-dashed"></div><br>
                              <div class="form-group">
                                  <label class="col-sm-2 control-label">日期<span class="red-fonts"></span></label>
                                  <div class="col-sm-5">
                                      <input  name="date" placeholder="*" type="text" class="form-control" >
                                  </div>
                              </div>
                              <div class="hr-line-dashed"></div><br>
                              <div class="form-group">
                                  <label class="col-sm-2 control-label">月份<span class="red-fonts"></span></label>
                                  <div class="col-sm-5">
                                      <input  name="month" placeholder="*" type="text" class="form-control" >
                                  </div>
                              </div>
                              <div class="hr-line-dashed"></div><br>
                              <div class="form-group">
                                  <label class="col-sm-2 control-label">星期<span class="red-fonts"></span></label>
                                  <div class="col-sm-5">
                                      <input  name="week" placeholder="*" type="text" class="form-control" >
                              </div>
                              </div>
                          </div>
                    </div>
                </div>
            </form>
            
        </div>
      </div>
      <div class="modal-footer">
        <button type="submit" class="btn btn-primary dim" form="assetForm" formmethod="post" formaction="{% url 'task_plan_manage' %}?project={{project}}&action=save" >保存</button>
        <button type="button" class="btn btn-danger dim" data-dismiss="modal">关闭</button>
      </div>
    </div>
  </div>
</div>

{%endblock%}